<?php

namespace App\Jobs;

use App\Helpers\PerFormanceTrait;
use App\Models\Bill;
use App\Models\BillLog;
use App\Models\PerformanceTable;
use App\Models\SystemUser;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class PerformanceJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, PerFormanceTrait;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $jobs = $this->getJob();
        $this->InserPerformance($jobs);
        $this->InserPerformance($jobs,1);
    }
    function getBillLogPrice($log_id){
        $id = Bill::query()->where('on_job_id',$log_id)->value('id');
        return BillLog::query()->where('bills_id',$id)
            ->where('status',2)
            ->sum('received');
    }
    public function InserPerformance($jobs, $type = null)
    {
        if ($jobs) {
            $firstOfMonth = new Carbon('first day of last month');
            $times = strtotime($firstOfMonth->startOfMonth()->toDateTimeString());
            $month = date("Ym", $times);
            foreach ($jobs as $key => $v) {
                //总充值
                $bill = $this->getBillLogPrice($v->id);
                $ratio = $type?$this->getUserRate($v->company_stakeholder_id):$this->getUserRate($v->school_stakeholder_id) ?? 0.00;
                $days = $this->getLastJobDays($v->checked_at,$v->post_id);//上个月上岗需要付费天数
                $all_days = $this->getOnJobAllDays($v->checked_at,$v->post_id);//总共上岗天数
                $departemnt = $type?$this->depratment($v->company_stakeholder_id):$this->depratment($v->school_stakeholder_id);
                if($type){
                    $last_performance = PerformanceTable::query()
                        ->where('channel_id',$v->company_stakeholder_id)
                        ->where('on_job_logs_id',$v->id)
                        ->count('total_performance');
                }else{
                    $last_performance = PerformanceTable::query()
                        ->where('channel_id',$v->school_stakeholder_id)
                        ->where('on_job_logs_id',$v->id)
                        ->count('total_performance');
                }

                $data[$key]['student_name'] = $v->student_name;
                $data[$key]['days'] = $days;
                $data[$key]['settlement_time'] = $this->endOfMonth();//结算时间
                $data[$key]['management_fee'] = $v->management_fee;//管理费
                $data[$key]['toll'] = $v->toll;
                $data[$key]['insurance'] = $v->insurance;
                $data[$key]['actual_price'] = $v->actual_price;
                $data[$key]['unit_price'] = $v->unit_price;
                $data[$key]['total_performance'] = $this->getTotal($bill,$all_days,$v,$last_performance);
                $data[$key]['channel_ratio'] = $ratio??0;//利率
                $data[$key]['channel_id'] = $type?$v->company_stakeholder_id:$v->school_stakeholder_id;//受益人id
                $data[$key]['employ_name'] = $type?$this->getSystemUser($v->company_stakeholder_id):$this->getSystemUser($v->school_stakeholder_id);
                $data[$key]['department'] =$departemnt;
                $data[$key]['month'] = $month;
                $data[$key]['created_at'] = now();
                $data[$key]['post_id'] = $v->post_id;
                $data[$key]['on_job_logs_id'] = $v->id;
                DB::table('performance_tables')->insert($data);


            }
        }
    }
    protected function depratment($uid){
        $user = SystemUser::query()->where('id',$uid)->with('department')->first();
        if($user){
            return $user->department->name;
        }else{
            return '';
        }

    }
    protected function getSystemUser($id){
        return SystemUser::query()->where('id',$id)->value('name');
    }
    private function this_day($post_id){
        $expire = $this->getExpire($post_id);
        if($expire==1){
            $this_day = 90;
        }else{
            $this_day = 30;
        }
        return $this_day;
    }

}
